//
//  NSDate+Calculator.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/4.
//  Copyright © 2016年 guozhzh. All rights reserved.
//

import Foundation

let lunarDayStrings = ["初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"]
let lunaMonthStrings = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一", "十二"]

public extension NSDate {
    
    @nonobjc static let MaxDate = NSDate(year: 2037, month: 12, day: 31)
    @nonobjc static let MinDate = NSDate(year: 1970, month: 1, day: 1)
    
    // 计算date是几号
    static func day(date: NSDate) -> NSInteger {
        let compontents = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: date)
        return compontents.day
    }
    
    // 计算date是几月
    static func month(date: NSDate) -> NSInteger {
        let compontents = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: date)
        return compontents.month
    }

    // 计算date是哪一年
    static func year(date: NSDate) -> NSInteger {
        let compontents = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: date)
        return compontents.year
    }

    // 计算一个月有几周
    static func totalWeeksInTheMonth(date: NSDate) -> NSInteger {
        let totalWeeks: NSRange = NSCalendar.currentCalendar().rangeOfUnit(.WeekOfMonth, inUnit: .Month, forDate: date)

        return totalWeeks.length
    }

    // 计算每个月的第一天是星期几
    static func firstDayInTheMonth(date: NSDate) -> NSInteger {
        let calendar = NSCalendar.currentCalendar()
        calendar.firstWeekday = 1
        let compontents = calendar.components([.Year, .Month, .Day], fromDate: date)
        compontents.day = 1
        let firstDayOfMonth = calendar.dateFromComponents(compontents)
        let firstWeekDay = calendar.ordinalityOfUnit(.Weekday, inUnit: .WeekOfMonth, forDate: firstDayOfMonth!)

        return firstWeekDay - 1

    }

    // 计算周几
    static func weekDay(date: NSDate) -> NSInteger {
        let compontents = NSCalendar.currentCalendar().components([.Year, .Month, .Weekday, .Day], fromDate: date)
        return compontents.weekday
    }

    // 获得当前周的第一天（周日）
    static func firstDayOfWeek(date: NSDate) -> NSDate {
        let components = NSDateComponents()

        components.day = -weekDay(date) + 1
        components.hour = 0
        components.minute = 0
        components.second = 0
        let firstday = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)

        return firstday!
    }

    // 获得当前周的最后一天（周六）
    static func lastDayOfWeek(date: NSDate) -> NSDate {
        let components = NSDateComponents()

        components.day = +6 - weekDay(date)
        components.hour = 24
        components.minute = 0
        components.second = 0
        let lastday = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)

        return lastday!
    }

    // 获得当前周的最后一天+1天
    static func lastPlus1DayOfWeek(date: NSDate) -> NSDate {
        let components = NSDateComponents()

        components.day = +7 - weekDay(date)
        components.hour = 24
        components.minute = 0
        components.second = 0
        let lastday = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)

        return lastday!
    }

    // 当前月份的天数
    static func totalDaysInTheMonth(date: NSDate) -> NSInteger {
        let totalDays: NSRange = NSCalendar.currentCalendar().rangeOfUnit(.Day, inUnit: .Month, forDate: date)

        return totalDays.length
    }

    // 上一个月
    static func theLastMonth(date: NSDate) -> NSDate {
        let components = NSDateComponents()

        components.month = -1
        let lastMonth = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)

        return lastMonth!

    }

    // 下一个月
    static func theNextMonth(date: NSDate) -> NSDate {
        let components = NSDateComponents()

        components.month = +1
        let nextMonth = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)

        return nextMonth!

    }

    // 前后N天的日期
    static func theNextDays(date: NSDate, day: Int) -> NSDate {
        let components = NSDateComponents()

        components.day = +day
        return NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)!
    }

    // 获取时间的农历String
    static func getLunaString(date: NSDate) -> String {
        let lunaCalendar = NSCalendar.init(calendarIdentifier: NSCalendarIdentifierChinese)
        lunaCalendar!.locale = NSLocale(localeIdentifier: "zh-CN")
        let lunaDay = lunaCalendar?.component(.Day, fromDate: date)
        if lunaDay == 1 {
            let lunaMonth = lunaCalendar?.component(.Month, fromDate: date)
            return lunaMonthStrings[lunaMonth! - 1]
        } else {
            return lunarDayStrings[lunaDay! - 1]
        }
    }

    // 一日之始
    static func beginOfDay(date: NSDate) -> NSDate {
        let components = NSDateComponents()
        components.hour = 0
        components.minute = 0
        components.second = 0
        return NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)!
    }

    // 一日之末
    static func endOfDay(date: NSDate) -> NSDate {
        let components = NSDateComponents()
        components.hour = 24
        components.minute = 0
        components.second = 0
        return NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: .MatchStrictly)!
    }

    static func getTimeStamp(date: NSDate) -> Int {
        return Int(date.timeIntervalSince1970 * 1000)
    }
}